// 当前项目使用的加解密方式
import { uuid } from '../utils/tools'

// #ifndef H5
import CryptoJS from 'crypto-js'
import JSEncrypt from './jsencrypt'
// #endif

// #ifdef H5
import JSEncrypt from 'jsencrypt'
import CryptoJS from 'crypto-es'
// #endif

// 初始向量 initial vector 16 位
const init_iv: string = 'E08ADE2699714B87'
const iv = CryptoJS.enc.Utf8.parse(init_iv)

const encrypt = {
  // AES 加密
  AESencrypt: (data: string, key: string) => {
    const srcs = CryptoJS.enc.Utf8.parse(data)
    const keys = CryptoJS.enc.Utf8.parse(key)
    const encrypted = CryptoJS.AES.encrypt(srcs, keys, {
      iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,
    })
    const result = CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
    const base64Data = CryptoJS.enc.Base64.parse(result)
    const HexData = CryptoJS.enc.Hex.stringify(base64Data)
    return HexData
  },
  // RSA 加密
  RSAencrypt: (publicKey: string, key: string) => {
    const encrypt = new JSEncrypt()
    encrypt.setPublicKey(publicKey)
    const encrypted = encrypt.encrypt(key)
    return encrypted
  },
  // RSA 解密
  RSADecrypt: (key: string) => {
    const encrypt = new JSEncrypt()
    encrypt.setPrivateKey(
      'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALg1hZ0JhNN8xApkIIMWZyWLBfFcU29bi3L14PkGwW0h+mAM97k9cevO6baddA5Aizq3FqZgA7ua0D/CHELwc8YI6qil9sVEgL5+wQag+AVoWxlKYnD+VYMPcuKgrSRL+r6d97N6ioiLRZJHJIQlBBKU1JJj743BZqvk+ECVc777AgMBAAECgYANDC10nBH+cN3p5oGvB00zCY/YMrPuNUvBTs0qlmHo+vkjCIgbZNDG5RcEyk5IHCZAeGtwSWi6RxLtETCzRRjOFPR/mtBrnNBsg31l4IWbSOsXs7TQOyaW0Rk7Yel/lwmzF57+PlQGTiQ07WpIJejEm3FfUoEXQ1sMIrcBBQnDwQJBAP9hrzzZQE9sJZjGYkH0KgpIaIoPkDEg82CBUKBKccjRhdGd45yMJOd5fDz7e5VRf6MtbjM/8q55MuFC+byJLusCQQC4p7dhZKM4lJRFR+KVT0+XhF9rCRe7SruZUpr7mBvREfEkeUER0ZLWgXdXvi3xCh8ew/8/VSzAhLflpPemzEwxAkBFOdCLuQRSwykJ2bTm1e3Tbb9QtVlLx/fREByv5p4jZpBUuuzpEaArUuYXjb/5QOdNb1Iw+EOrgdUF9ObHcZRTAkBvl7v1OdKCQRiR/e1q+nFJShA4KViUMYfKEufioHw/Dy7CMh0m3ZZ11+v9tMsi3lEMULqS1bCj8BEK0XVw8gohAkEApXvjjYRCfV9DnEa7Z0s72QvSXZU4aq6G6KUjboSj0Tz0Rh1OSePkz411cvtM3Q3zBhKQS5qzaqj0YQoSDjH+Lg=='
    )
    const encrypted = encrypt.decrypt(key)
    return encrypted.toString()
  },
  // AES 解密
  AESDecrypt: (data: string, uuid: string) => {
    const keys = CryptoJS.enc.Utf8.parse(uuid)
    const cipherText = CryptoJS.enc.Base64.stringify(
      CryptoJS.enc.Hex.parse(data)
    )
    const decrypt = CryptoJS.AES.decrypt(cipherText, keys, {
      iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,
    })
    const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
    return decryptedStr.toString()
  },
  // 加密
  encrypt: (data: UniApp.RequestOptions['data'], publicKey: string) => {
    let jsonStr = ''
    if (typeof data == 'string') {
      jsonStr = data
    } else {
      jsonStr = (data && JSON.stringify(data)) || ''
    }
    const key = uuid()
    const aesCipher = encrypt.AESencrypt(jsonStr, key)

    const paramsKey = encrypt.RSAencrypt(publicKey, key)
    return {
      // data: aesCipher,
      x_enc: aesCipher,
      key: paramsKey,
      resKey: key,
    }
  },
  decrypt: (data: string, key: string) => {
    // const rsaRes = encrypt.RSADecrypt(key)
    // const aesRes = encrypt.AESDecrypt(data, rsaRes)
    const aesRes = encrypt.AESDecrypt(data, key)
    return (aesRes && JSON.parse(aesRes)) || null
  },
}

export default encrypt
